package com.liruo.demojsrjpa.repository;

import com.liruo.demojsrjpa.model.dto.DqlEntityInterfaceDto;
import com.liruo.demojsrjpa.model.entity.DqlEntity;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface QueryMethodRepository extends
    JpaRepository<DqlEntity, Long> {
  <T> Optional<T> findByName(String name, Class<T> type);
  Optional<DqlEntityInterfaceDto> findByName(String name);
  @Query(value = "select age as age, name as name from DqlEntity where age = :age")
  Optional<DqlEntityInterfaceDto> selectByQueryAndSpringProject(@Param("age") int age);
  @Query(value = "select new DqlEntity(name, age) from DqlEntity where age = :age")
  Optional<DqlEntity> selectByHQLNew(@Param("age") int age);

  @Query(value = "select * from `demo-jsr-jpa`.dql_entity where age > :age and  age != :aage", nativeQuery = true)
  Optional<DqlEntity> sqlByQueryNameParam(@Param("age") int age, @Param("aage") int aage);

  @Query(value = "from DqlEntity where age > :age and  age <> :aage")
  Optional<DqlEntity> hqlByQueryNameParam(@Param("age") int age, @Param("aage") int aage);

  @Query(value = "from DqlEntity where age > ?1 and  age <> ?2")
  Optional<DqlEntity> hqlByQueryIndexParam(int age, int aage);

}
